package com.base.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.PayinfoesExamplePage;
import com.base.po.voip.Payinfoes;
import com.base.po.voip.Users;
import com.base.po.voip.PayinfoesExample.Criteria;
import com.base.service.LogsService;
import com.base.service.PayinfoesService;
import com.base.service.UsersService;
import com.base.utils.DateUtil;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_chongzhibaobiao {
	@Autowired
	private PayinfoesService payinfoesService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private LogsService logsService;// log日志

	// 充值报表
	@RequestMapping("/content_chongzhibaobiao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		PayinfoesExamplePage examplePage = new PayinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("PayTime");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<String> list = null;
		List<Payinfoes> payinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		switch (adminUsergrade) {
		case 0:
			break;

		case 1:
			// 查询OEMID下的用户
			list = usersService.selectByParentId(adminUserId);
			for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			// 查询商家下的ID
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		}
		payinfoes = payinfoesService.selectByExamplePage(examplePage);
		if (payinfoes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = payinfoesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 传入session中
			request.setAttribute("curPage", 1);// 传入session中
			request.setAttribute("date", "01/01/2019 -  ");
			
			if (payinfoes != null) {
				for (Payinfoes r : payinfoes) {
					// 设置OEM以及所属商家的账户名
					Users parent = usersService.selectByPrimaryKey(r.getParentid());
					if (parent != null && parent.getUsername() != null) {
						r.setParentid(parent.getUsername());
					} else {
						r.setParentid("未知");
					}
				
				}
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_chongzhibaobiao");
			request.setAttribute("list", payinfoes);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "充值报表", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 充值报表
	@RequestMapping("/selectPayinfoes")
	public ModelAndView selectPayinfoes(HttpServletRequest request, String id, String mobile, String type,
			Integer money, Integer curPage, String date) throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2019 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		if (type == null) {
			if (request.getSession().getAttribute("type") != null) {
				type = (String) request.getSession().getAttribute("type");
			}
		}
		if (money == null) {
			if (request.getSession().getAttribute("money") != null) {
				money = (Integer) request.getSession().getAttribute("money");
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		PayinfoesExamplePage examplePage = new PayinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("PayTime");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<String> list = null;
		List<Payinfoes> payinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (id != null && !id.equals("")) {
			request.setAttribute("id", id);
			criteria = criteria.andIdEqualTo(id);
			examplePage.getOredCriteria().add(criteria);
		}
		if (mobile != null && !mobile.equals("")) {
			request.setAttribute("mobile", mobile);
			criteria = criteria.andMobileEqualTo(mobile);
			examplePage.getOredCriteria().add(criteria);
		}
		System.out.println(mobile);
		if (type != null && type.contains("卡")) {
			request.setAttribute("type", type);
			if(type.contains("话费")) {
			    criteria = criteria.andTypeEqualTo(1).andCardtypeEqualTo(1);
			}else if(type.contains("购物")) {
				criteria = criteria.andTypeEqualTo(1).andCardtypeEqualTo(2);
			}else if(type.contains("等值")){
				criteria = criteria.andTypeEqualTo(1).andCardtypeEqualTo(3);
			}else{
				criteria = criteria.andTypeEqualTo(1);
			}
			examplePage.getOredCriteria().add(criteria);
		}
		if (type != null && type.contains("空中")) {
			request.setAttribute("type", type);
			if(type.contains("话费")) {
			    criteria = criteria.andTypeEqualTo(2).andCardtypeEqualTo(1);
			}else if(type.contains("购物")) {
				criteria = criteria.andTypeEqualTo(2).andCardtypeEqualTo(2);
			}else {
				criteria = criteria.andTypeEqualTo(2);
			}
			examplePage.getOredCriteria().add(criteria);
		}
		if (type != null && type.contains("在线")) {
			request.setAttribute("type", type);
			criteria = criteria.andTypeBetween(3,5);
			examplePage.getOredCriteria().add(criteria);
		}
		if (money != null) {
			request.setAttribute("money", money);
			criteria = criteria.andMoneyEqualTo(money);
			examplePage.getOredCriteria().add(criteria);
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			System.out.println(sourceStrArray[i]);
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			request.setAttribute("date", date);
			if (dateTo != null) {
				criteria = criteria.andPaytimeBetween(dateFrom, dateTo);
			} else {
				criteria = criteria.andPaytimeBetween(dateFrom, Produce.getNowDate());
			}
			examplePage.getOredCriteria().add(criteria);
		}
		switch (adminUsergrade) {
		case 0:
			break;

		case 1:
			// 查询OEMID下的用户
			list = usersService.selectByParentId(adminUserId);
			if(mobile == null || mobile.equals("")){
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId));
				}
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			// 查询商家下的ID
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;
		}
		payinfoes = payinfoesService.selectByExamplePage(examplePage);
		if (payinfoes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = payinfoesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 总数据传入session中
			request.setAttribute("curPage", curPage);// 当前页面传入session中

			if (payinfoes != null) {
				for (int i = 0;i < payinfoes.size(); i++) {
					// 设置OEM以及所属商家的账户名
					Users parent = usersService.selectByPrimaryKey(payinfoes.get(i).getParentid());
					if (parent != null && parent.getUsername() != null) {
						payinfoes.get(i).setParentid(parent.getUsername());
					} else {
						payinfoes.get(i).setParentid("未知");
					}
				}
			}

			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_chongzhibaobiao");
			request.setAttribute("list", payinfoes);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/content_duizhangdan")
	public ModelAndView content_duizhangdan(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		PayinfoesExamplePage examplePage = new PayinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("PayTime");
		Users users = usersService.selectByPrimaryKey(id);
		Integer adminUsergrade = users.getRole();
		String adminUserId = id;
		List<Payinfoes> payinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		switch (adminUsergrade) {
		case 0:
			break;

		case 1:
			// 查询OEMID下的用户
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			// 查询商家下的ID
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		}
		payinfoes = payinfoesService.selectByExamplePage(examplePage);
		if (payinfoes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = payinfoesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 传入session中
			request.setAttribute("curPage", 1);// 传入session中
			request.setAttribute("date", "01/01/2019 -  ");
			request.setAttribute("id", id);
			
			if (payinfoes != null) {
				for (Payinfoes r : payinfoes) {
					// 设置OEM以及所属商家的账户名
					Users parent = usersService.selectByPrimaryKey(r.getParentid());
					if (parent != null && parent.getUsername() != null) {
						r.setParentid(parent.getUsername());
					} else {
						r.setParentid("未知");
					}
				
				}
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_duizhangdan");
			request.setAttribute("list", payinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "充值报表", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/selectPayinfoesById")
	public ModelAndView selectPayinfoesById(HttpServletRequest request, String id, String mobile, String type,
			Integer money, Integer curPage, String date) throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2019 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		if (type == null) {
			if (request.getSession().getAttribute("type") != null) {
				type = (String) request.getSession().getAttribute("type");
			}
		}
		if (money == null) {
			if (request.getSession().getAttribute("money") != null) {
				money = (Integer) request.getSession().getAttribute("money");
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		PayinfoesExamplePage examplePage = new PayinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("PayTime");
		Users users = usersService.selectByPrimaryKey(id);
		Integer adminUsergrade = users.getRole();
		String adminUserId = id;
		List<Payinfoes> payinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (mobile != null && !mobile.equals("")) {
			request.setAttribute("mobile", mobile);
			criteria = criteria.andMobileLike("%" + mobile + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		if (type != null && type.contains("卡")) {
			request.setAttribute("type", type);
			if(type.contains("话费")) {
			    criteria = criteria.andTypeEqualTo(1).andCardtypeEqualTo(1);
			}else if(type.contains("购物")) {
				criteria = criteria.andTypeEqualTo(1).andCardtypeEqualTo(2);
			}else {
				criteria = criteria.andTypeEqualTo(1);
			}
			examplePage.getOredCriteria().add(criteria);
		}
		if (type != null && type.contains("空中")) {
			request.setAttribute("type", type);
			if(type.contains("话费")) {
			    criteria = criteria.andTypeEqualTo(2).andCardtypeEqualTo(1);
			}else if(type.contains("购物")) {
				criteria = criteria.andTypeEqualTo(2).andCardtypeEqualTo(2);
			}else {
				criteria = criteria.andTypeEqualTo(2);
			}
			examplePage.getOredCriteria().add(criteria);
		}
		if (type != null && type.contains("在线")) {
			request.setAttribute("type", type);
			criteria = criteria.andTypeEqualTo(3);
			examplePage.getOredCriteria().add(criteria);
		}
		if (money != null) {
			request.setAttribute("money", money);
			criteria = criteria.andMoneyEqualTo(money);
			examplePage.getOredCriteria().add(criteria);
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			System.out.println(sourceStrArray[i]);
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			request.setAttribute("date", date);
			if (dateTo != null) {
				criteria = criteria.andPaytimeBetween(dateFrom, dateTo);
			} else {
				criteria = criteria.andPaytimeBetween(dateFrom, Produce.getNowDate());
			}
			examplePage.getOredCriteria().add(criteria);
		}
		switch (adminUsergrade) {
		case 0:
			break;

		case 1:
			// 查询OEMID下的用户
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			// 查询商家下的ID
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;
		}
		payinfoes = payinfoesService.selectByExamplePage(examplePage);
		if (payinfoes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = payinfoesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 总数据传入session中
			request.setAttribute("curPage", curPage);// 当前页面传入session中
			
			if (payinfoes != null) {
				for (Payinfoes r : payinfoes) {
					// 设置OEM以及所属商家的账户名
					Users parent = usersService.selectByPrimaryKey(r.getParentid());
					if (parent != null && parent.getUsername() != null) {
						r.setParentid(parent.getUsername());
					} else {
						r.setParentid("未知");
					}
				
				}
			}
			
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_duizhangdan");
			request.setAttribute("list", payinfoes);
			request.setAttribute("id", id);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
